﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using SharpFileZillaStats.Properties;
using SharpMap;
using SharpMap.Data.Providers;

namespace SharpFileZillaStats {
    public partial class DownloadMap : UserControl {

        public StatsReader SR{get;set;}
        private KeyEventArgs pressedKeys = null;

        private SharpMap.Geometries.BoundingBox Extend = null;
        public DownloadMap() {
            InitializeComponent();
            this.init();
        }

        
        private void init() {
            cbActiveLabel.Items.AddRange(Enum.GetNames(typeof(SharpMap.Forms.MapBox.Tools)));
            cbActiveLabel.SelectedIndex = 0;
        }


        public void drawMap(Object sender, EventArgs e) {
            drawMap();
        }

        public void drawMap(){
            if(this.SR == null) return;

           
            MapBox.Map.Layers.Clear();
            SharpMap.Layers.VectorLayer vl = new SharpMap.Layers.VectorLayer("Test");

            String pathSHP = (Path.IsPathRooted(Settings.Default.DefaultShapeFile)) ? 
                Settings.Default.DefaultShapeFile : 
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"data","shp", Settings.Default.DefaultShapeFile);
            //SharpMap.Data.Providers.ShapeFile sf = 
            //new SharpMap.Data.Providers.ShapeFile(
            //@"C:\Users\Benjamin\Documents\SVN_CheckOuts\SharpFileZillaStats\data\shp\continents.shp", true);
            ShapeFile sf = new ShapeFile(pathSHP, true);
            int c = sf.GetFeatureCount();
            vl.DataSource = sf;
            vl.Style.Fill = new SolidBrush(Color.FromArgb(200, 200, 200));
            vl.Style.Enabled = true;
            vl.Style.EnableOutline = true;
            vl.Style.Outline = new Pen(Color.FromArgb(50,50,50), .5f);

            //get number of downloads per position
            var points = (from row in SR.DataView.ToTable().AsEnumerable()
                         let point = new SharpMap.Geometries.Point(
                                 row.Field<Double>(SR.ColumnLongitude),
                                 row.Field<Double>(SR.ColumnLatitude)
                                 )
                         select point).Distinct();

            Collection<SharpMap.Geometries.Geometry> gc3 = new Collection<SharpMap.Geometries.Geometry>();
            foreach(var p in points) gc3.Add(p);

            SharpMap.Data.Providers.GeometryProvider gp = new GeometryProvider(gc3);
            
            SharpMap.Layers.VectorLayer vlPoints = new SharpMap.Layers.VectorLayer("Donwload Locations");
            vlPoints.DataSource = gp;
            //vlPoints.Style.Symbol = SharpMap.Styles.VectorStyleDefaultSymbol;
            SharpMap.Styles.VectorStyle vs = new SharpMap.Styles.VectorStyle();
            vs.Fill = new SolidBrush(Color.FromArgb(0,0,255));
            vs.PointColor =  new SolidBrush(Color.FromArgb(200,0,0));
            vs.PointSize = 5.0f;
            vs.Outline = new Pen(Color.Red, 10);
            vlPoints.Style = vs;
                // vlPoints.Style.PointColor = new SolidBrush(Color.FromArgb(255, 0, 0));


            MapBox.Map.Layers.Add(vl);  
            MapBox.Map.Layers.Add(vlPoints);


            if(this.Extend != null) {
                MapBox.Map.ZoomToBox(this.Extend);
            } else { 
                MapBox.Map.ZoomToExtents();
            }
            
            //this.Extend = MapBox.Map.GetExtents();
            MapBox.Refresh();
        }

        private void cbActiveLabel_SelectedIndexChanged(object sender, EventArgs e) {
            SharpMap.Forms.MapBox.Tools tool = parseEnum<SharpMap.Forms.MapBox.Tools>(cbActiveLabel);
            this.MapBox.ActiveTool = tool;
        }

        private T parseEnum<T>(ToolStripComboBox comboBox) {
            return (T)Enum.Parse(typeof(T), comboBox.SelectedItem.ToString());
        }
        private T parseEnum<T>(ComboBox comboBox) {
            return (T)Enum.Parse(typeof(T), comboBox.SelectedItem.ToString());
        }

        private void mapBox1_MouseClick(object sender, MouseEventArgs e) {
           
           
        }

        
        private void mapBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
            //Center the map on the click-point
            Map map = MapBox.Map;
            map.Center = map.ImageToWorld(new System.Drawing.PointF(e.X, e.Y), true);
            //Zoom in 2x
            if(MapBox.ActiveTool == SharpMap.Forms.MapBox.Tools.ZoomIn) {
                //    map.Zoom *= 0.8;
            }
            //Call function that renders the map and returns it to the client
            MapBox.Refresh();
        }

        private void mapBox1_KeyUp(object sender, KeyEventArgs e) {
            this.pressedKeys = null;
            MapBox.ActiveTool = parseEnum<SharpMap.Forms.MapBox.Tools>(cbActiveLabel);
        }

        private void MapBox_KeyDown(object sender, KeyEventArgs e) {
            this.pressedKeys = e;

            switch(e.KeyCode) { 
                case Keys.ShiftKey:
                    MapBox.ActiveTool = SharpMap.Forms.MapBox.Tools.ZoomOut;
                    break;
                case Keys.ControlKey:
                    MapBox.ActiveTool = SharpMap.Forms.MapBox.Tools.ZoomIn;
                    break;
                default:
                    break;
            }
        }


        private void MapBox_Leave(object sender, EventArgs e) {
            if(MapBox.Map.Envelope.Width > 0) this.Extend = MapBox.Map.Envelope;
        }

        
    }
}
